package dbb

import (
	"fyne.io/fyne/v2"
	"fyne.io/fyne/v2/container"
	"fyne.io/fyne/v2/widget"
	"github.com/gogf/gf/v2/container/garray"
	"github.com/gogf/gf/v2/database/gdb"
	"github.com/gogf/gf/v2/util/gconv"
	"github.com/gogf/gf/v2/util/gutil"
	"github.com/y2h/fyne_poem/widgets"
)

func idx(db gdb.DB, tbl string) fyne.CanvasObject {
	xdb := gf2xorm(db)
	idx, _ := xdb.Dialect().GetIndexes(xdb.DB(), Ctx, tbl)
	idxs := garray.NewArrayFrom(gutil.Values(idx))
	if idxs.Len() == 0 {
		return widget.NewLabel("no indexes")
	}
	idx0 := idxs.At(0)
	// 获取schemas.Index结构体属性
	fds := gutil.Keys(idx0)
	numcols := len(fds)
	lst := widget.NewList(func() int {
		return idxs.Len()
	}, func() fyne.CanvasObject {
		grid := container.NewGridWithColumns(numcols)
		for i := 0; i < numcols; i++ {
			grid.Add(widget.NewLabel("indexes"))
		}
		return grid
	}, func(lii widget.ListItemID, co fyne.CanvasObject) {
		grid := co.(*fyne.Container)
		lbls := grid.Objects
		idxi := idxs.At(lii)
		for i, v := range lbls {
			lbl := v.(*widget.Label)
			va, found := gutil.ItemValue(idxi, fds[i])
			if found {
				lbl.SetText(gconv.String(va))
			}
		}
	})

	return container.NewBorder(widgets.Header(fds, 0), nil, nil, nil, lst)
}
